<template>
	<view class="container home-bg home-pd home-h flex flex-col">
		<customInput @confirm="confirm" v-model="params.name" />
		<view class="goodsList-container flex-1">
			<goodsListItem :item="item" v-for="(item,index) in list" :key="item.id" />
		</view>
	</view>
</template>

<script setup>
	import customInput from '../components/custom-input';
	import goodsListItem from '../components/goods-list-item';
	import {
		onLoad,
		onReachBottom,
		onPullDownRefresh,
		onShow
	} from '@dcloudio/uni-app';

	import {
		getlist
	} from '@/api/goods.js';
	let params = reactive({
		name: "",
		auditStatus: 1,
		pageNo: 1,
		pageSize: 10,
		enableMark: 0,
		providerModel: "SUPPLIER"
	});
	let list = ref([]);
	 onShow(()=>{ 
		 if(list.value.length){
			 setList({
				 ...params,
				 pageNo:1,
				 pageSize:list.value.length
			 });
		 }
	 });
	onLoad(() => {
		init()
	});
	onReachBottom(async () => {
		params.pageNo += 1;
		setList()
	})
	onPullDownRefresh(() => {
		params.pageNo = 1;
		init()
	})
	async function init() { 
		setList()
	}
	async function confirm(e) { 
		params.name = e;
		params.pageNo = 1;
		setList()
	}
	async function setList(e) {
		list.value = await getList(e);
	}

	function getList(requestData = params) { 
		return new Promise((re, rj) => {
			getlist(requestData).then(res => {
				uni.stopPullDownRefresh();
				if (!res.data.list) {
					uni.showToast({
						title: '没有数据了',
						icon: 'none'
					})
				}
				let {
					data = {
						list: []
					}
				} = res;
				re(requestData.pageNo != 1 ? list.value.concat(data.list) : data.list);
			});
		})

	}
</script>

<style lang="scss" scoped>
	@import 'index.scss';
</style>